﻿using DataStructureSample.ListDs;
using DataStructureSample.Sorts;
using DataStructureSample.StackDs;
using System;
using System.Collections.Generic;
using System.Threading;

namespace DataStructureSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //SeqListTest();
                //StackTest();
                //QueueTest();
                //SeqListTest();
                //SeqStackTest();
                //PlalinDromePractice();
                //InsertSortTest();
                QuickSortTest();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message},{ex.StackTrace}");
            }
            Console.WriteLine("Hello World!");
            Console.Read();
        }

        private static void QuickSortTest()
        {
            int[] arr = new int[] { 2000, 10, 70, 555, 3 };
            QuickSort.Sort(arr,0,4);
        }

        private static void InsertSortTest()
        {
            int[] arr = new int[] { 2000, 10, 70, 555, 3 };
            var rt = InsertSort.Sort(arr);
            foreach (var item in rt)
            {
                Console.WriteLine(item);
            }
        }

        private static void PlalinDromePractice()
        {
            while (true)
            {
                var input = Console.ReadLine();

                var b = IsPlainDrome(input);
                Console.WriteLine($"{input} is plaindrome?{b}.");
            }
        }

        private static bool IsPlainDrome(string input)
        {
            Queue<char> queue = new Queue<char>();
            Stack<char> stack = new Stack<char>();
            foreach (var item in input.ToCharArray())
            {
                queue.Enqueue(item);
                stack.Push(item);
            }

            var rt = true;
            while (stack.Count > 0)
            {
                if (queue.Dequeue() != stack.Pop())
                {
                    rt = false;
                    break;
                }
            }
            return rt;
        }

        private static void SeqStackTest()
        {
            var stack = new SeqStack<string>();
            for (int i = 0; i < 10; i++)
            {
                stack.Push(Guid.NewGuid().ToString("N"));
            }
            Console.WriteLine(stack.Count);
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Count);
            Console.WriteLine(stack.Peek());
            Console.WriteLine(stack.GetLength());
            stack.Clear();
        }

        private static void QueueTest()
        {
            var queue = new Queue<int>();

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(100);
                var rd = new Random(DateTime.Now.Second);
                var iVal = rd.Next(1, 100);
                queue.Enqueue(iVal);
            }
            Console.WriteLine(queue.Count);
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Count);
            Console.WriteLine(queue.Peek());
            Console.WriteLine(queue.Count);
            queue.Clear();
            Console.WriteLine(queue.Count);
        }

        private static void StackTest()
        {
            Stack<string> stack = new Stack<string>();
            for (int i = 0; i < 5; i++)
            {
                stack.Push(Guid.NewGuid().ToString("N"));
            }
            Console.WriteLine(stack.Count);
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Count);
            Console.WriteLine("//************");
            Console.WriteLine(stack.Peek());
            Console.WriteLine(stack.Count);
            Console.WriteLine("//************");
        }

        private static void SeqListTest()
        {
            var seq = new SeqList<string>();
            for (int i = 0; i < 10; i++)
            {
                seq.Add(Guid.NewGuid().ToString("N"));
            }
            PrintResult(seq);

            seq.Insert("gainorloss", 1);
            PrintResult(seq);

            seq.Delete(2);
            PrintResult(seq);

            Console.WriteLine(seq.Locate("gainorloss"));

            Console.WriteLine(seq.IsEmpty());
            seq.Clear();
            Console.WriteLine(seq.IsEmpty());
        }

        private static void PrintResult(SeqList<string> seq)
        {
            Console.WriteLine("------------------------");
            for (int i = 0; i < seq.GetLength(); i++)
            {
                Console.WriteLine(seq[i]);
            }
        }
    }
}
